package com.lsmw.order.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lsmw.commonutils.R;
import com.lsmw.order.entity.LsOrder;
import com.lsmw.order.service.LsOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 三号男嘉宾
 * @since 2021-03-19
 */
@CrossOrigin
@Api(tags = "订单信息")
@RestController
@RequestMapping("/order/ls-order")
public class LsOrderController {

    private LsOrderService lsOrderService;

    @Autowired
    public LsOrderController(LsOrderService lsOrderService) {
        this.lsOrderService = lsOrderService;
    }

    @ApiOperation(value = "获取所有订单信息")
    @GetMapping("/")
    public R getOrders() {
        return lsOrderService.getOrders();
    }

    @ApiOperation(value = "根据用户id获取其所有订单信息")
    @GetMapping("/{userId}")
    public R getOrdersByUserId(
            @ApiParam(name = "userId", value = "用户id", required = true)
            @PathVariable String userId,
            HttpServletResponse response) {
        return lsOrderService.getOrdersByUserId(Long.valueOf(userId)).setResponseCode(response);
    }

    @ApiOperation(value = "根据订单id获取其所有订单信息")
    @GetMapping("orderId/{orderId}")
    public R getOrdersByOrderId(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable String orderId,
            HttpServletResponse response) {
        return R.ok().data("row", lsOrderService.getById(orderId));
    }

    @ApiOperation(value = "添加订单信息")
    @PostMapping("/")
    public R addOrder(
            @ApiParam(name = "lsOrderInfo", value = "订单信息", required = true)
            @RequestBody LsOrder lsOrder,
            HttpServletResponse response) {
        return lsOrderService.addOrder(lsOrder).setResponseCode(response);
    }

    @ApiOperation(value = "修改订单信息")
    @PutMapping("/")
    public R updateOrder(
            @ApiParam(name = "lsOrderInfo", value = "订单信息", required = true)
            @RequestBody LsOrder lsOrder,
            HttpServletResponse response) {
        return lsOrderService.updateOrder(lsOrder).setResponseCode(response);
    }

    @ApiOperation(value = "根据订单id删除订单信息")
    @DeleteMapping("/{orderId}")
    public R deleteOrderByOrderId(
            @ApiParam(name = "orderId", value = "订单id", required = true)
            @PathVariable String orderId,
            HttpServletResponse response) {
        return lsOrderService.deleteOrderByOrderId(orderId).setResponseCode(response);
    }

    @ApiOperation(value = "根据多个订单id删除订单信息")
    @DeleteMapping("/deleteOrderByOrderIds")
    public R deleteOrderByOrderIds(
            @ApiParam(name = "orderIds", value = "多个订单id", required = true)
            @RequestParam("orderIds") String orderIds,
            HttpServletResponse response) {
        String[] ids = orderIds.split(",");
        List<String> idList = new ArrayList<>();
        for (String id : ids) {
            idList.add(id);
        }
        return lsOrderService.deleteOrderByOrderIds(idList).setResponseCode(response);
    }

    @ApiOperation(value = "分页查询")
    @GetMapping("/pageOrder/{current}/{limit}")
    public R getHotelListPage(
            @ApiParam(name = "current", value = "当前页下标", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit) {
        Page<LsOrder> orderPage = new Page<>(current, limit);
        lsOrderService.page(orderPage, null);
        Map map = new HashMap();
        map.put("total", orderPage.getTotal());
        map.put("rows", orderPage.getRecords());
        return R.ok().data(map);
    }

    @ApiOperation(value = "根据订单号查询订单信息")
    @GetMapping("/order/{id}/{current}/{limit}")
    public R getHotelInfoById(
            @ApiParam(name = "id", value = "订单号", required = true) @PathVariable String id,
            @ApiParam(name = "current", value = "当前页下标", required = true) @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable long limit) {
        Page<LsOrder> orderPage = new Page<>(current, limit);
        if (id == null || id == "") {
            lsOrderService.page(orderPage, null);
        } else {
            lsOrderService.page(orderPage, new LambdaQueryWrapper<LsOrder>().like(LsOrder::getOrderNo, id));
        }
        return R.ok().data("rows", orderPage.getRecords()).data("total", orderPage.getTotal());
    }

}

